package com.liyuan.help_e.config;

import com.liyuan.help_e.component.security.JwtAuthenticationEntryPoint;
import com.liyuan.help_e.component.security.JwtAuthenticationTokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Bean
    BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }
    @Autowired
    @Qualifier("securityUserDetailsService")
    UserDetailsService securityUserDeatilsService;
    @Autowired
    AccessDeniedHandler accessDeniedHandler;

    @Autowired
    JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
    @Autowired
    JwtAuthenticationTokenFilter authenticationTokenFilter;

    @Autowired
    AuthenticationSuccessHandler authenticationSuccessHandler;
//    @Autowired
//    AuthenticationFailureHandler authenticationFailureHandler;

    @Autowired
    LogoutSuccessHandler logoutSuccessHandler;


    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        http.headers().frameOptions().disable();

        http.authorizeRequests().antMatchers("/publish","/publish.html","/personal/**","/personal.html").hasAuthority("/user");
        http.formLogin().loginPage("/login").successHandler(authenticationSuccessHandler).failureUrl("/login?error").and().
//                failureHandler(authenticationFailureHandler).
        logout().logoutSuccessHandler(logoutSuccessHandler);
        http.exceptionHandling().accessDeniedHandler(accessDeniedHandler);
        //可以使用spring security 默认的权限不足处理，即直接跳转到登录页面...
//        http.exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint);
        http.userDetailsService(securityUserDeatilsService);
        http.logout().logoutSuccessUrl("/");
        //关闭session
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        //关闭缓存
        http.headers().cacheControl();

        http.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
//        http.addFilterBefore(customAuthenticationFilter(),UsernamePasswordAuthenticationFilter.class);
        http.authorizeRequests().anyRequest().permitAll();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/css/**","/js/**","/webjars/**","/images/**","/fonts/**");
    }
}
